package com.lyx.crm.controller;

import com.lyx.crm.annotation.RequiredPermission;
import com.lyx.crm.base.BaseController;
import com.lyx.crm.base.ResultInfo;
import com.lyx.crm.model.TreeModel;
import com.lyx.crm.service.ModuleService;
import com.lyx.crm.vo.Module;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.lyx.crm.controller
 * @ClassName: ModuleController
 * @Author: lyx
 * @CreateTime: 2021/9/6 15:44
 * @Description:
 */
@Controller
@RequestMapping("module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    /**
     * 查询所有资源列表
     *
     * @return
     */
    @RequestMapping("queryAllModules")
    @ResponseBody
    public List<TreeModel> queryAllModules(Integer roleId) {

        return moduleService.queryAllModules(roleId);
    }

    @RequestMapping("toAddGrantPage")
    public String toAddGrantPage(Integer roleId, HttpServletRequest request) {
        request.setAttribute("roleId", roleId);
        return "role/grant";
    }

    @RequiredPermission(code = "6030")
    @RequestMapping("index")
    public String index() {
        return "module/module";
    }

    /**
     * 查询条件查询所有资源
     *
     * @return
     */
    @RequiredPermission(code = "603002")
    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> queryModuleByParams() {

        return moduleService.queryModuleList();
    }

    /**
     * 添加资源
     *
     * @param module
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public ResultInfo addModule(Module module) {

        moduleService.addModule(module);
        return success("资源添加成功!");
    }

    /**
     * 返回添加资源页面
     *
     * @param grade
     * @param parentId
     * @param request
     * @return
     */
    @RequestMapping("toAddModulePage")
    public String toAddModulePage(Integer grade, Integer parentId, HttpServletRequest request) {
        request.setAttribute("grade", grade);
        request.setAttribute("parentId", parentId);
        return "module/add";
    }

    /**
     * 返回资源修改页面
     *
     * @param moduleId
     * @param request
     * @return
     */
    @RequestMapping("toUpdateModulePage")
    public String toUpdateModulePage(Integer moduleId, HttpServletRequest request) {
        Module module = moduleService.queryModuleByPrimaryId(moduleId);

        request.setAttribute("module", module);
        return "module/update";
    }

    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteModule(Integer moduleId) {
        moduleService.deleteModuleByPrimaryKey(moduleId);

        return success("资源删除成功！");
    }
}